package com.banzhi.shop.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banzhi.shop.R;
import com.banzhi.shop.base.BaseActivity;
import com.banzhi.shop.utils.BitmapUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.banzhi.shop.utils.Translucent;
import com.bm.library.PhotoView;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 图片查看器
 */
public class ViewPagerActivity extends BaseActivity {
    private ViewPager mPager;
    private ImageView ivback;
    private TextView num;
    private TextView allnum;
    private RelativeLayout rltitle;
    private ImageView ivsave;
    private List<String> images = new ArrayList<>();
    private int position;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_pager);
        Translucent.setTranslucent(this);
        Intent intent = getIntent();
        images.addAll((Collection<? extends String>) intent.getSerializableExtra("images"));
        position = intent.getIntExtra("position", 0);
        setinits();
        serListeners();
        showViews();
    }

    private void showViews() {
        num.setText(mPager.getCurrentItem() + 1 + "");
        allnum.setText(images.size() + "");
    }


    private void setinits() {
        mPager = (ViewPager) findViewById(R.id.pager);
        ivback = (ImageView) findViewById(R.id.iv_back);
        ivsave = (ImageView) findViewById(R.id.iv_save);
        num = (TextView) findViewById(R.id.num);
        allnum = (TextView) findViewById(R.id.allnum);
        rltitle = (RelativeLayout) findViewById(R.id.rl_title);
        mPager.setPageMargin((int) (getResources().getDisplayMetrics().density * 15));

        mPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return images.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                PhotoView view = new PhotoView(ViewPagerActivity.this);
                view.enable();
                view.setScaleType(ImageView.ScaleType.FIT_CENTER);
                Picasso.with(getApplication()).load(images.get(position)).placeholder(R.mipmap.vp_zhanweitu).into(view);
                container.addView(view);
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (rltitle.getVisibility() == View.INVISIBLE) {
                            rltitle.setVisibility(View.VISIBLE);
                        } else {
                            rltitle.setVisibility(View.INVISIBLE);
                        }
                    }
                });
                return view;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }
        });
        mPager.setCurrentItem(position);
    }

    private void serListeners() {
        ivback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                num.setText(position + 1 + "");
                ViewPagerActivity.this.position = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        ivsave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BitmapUtils.saveImageToGallery(ViewPagerActivity.this, BitmapUtils.returnBitmap(images.get(position)));
                ToastUtils.makeToastShort("保存图片成功");
            }
        });
    }
}
